//
//  LogInViewController.m
//  RentingCar
//
//  Created by wmwMAC on 17/4/6.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "LogInViewController.h"

#import "SinglePhoneNumberView.h"//
#import "PasswordView.h"

#import "ForgetPasswordViewController.h"//忘记密码
#import "ResignViewController.h"//注册

#import "MainViewController.h"//首页

//#import "BaseTabBarController.h"

#define phoneNumber_height (kUIScreenHeight-kNavigationBarHeight)/2.15
#define biaoImg_weidth kUIScreenHeight/4
@interface LogInViewController ()
{
    NSString *phoneNumberStr;
    
    
    NSString *PasswordStr;
}

@end

@implementation LogInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""]forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;
    
    [self addNextBtnUI];
    
    
}

-(void)addNextBtnUI{
    
    
    //图标
    UIImageView *biaoImgView = [MyUtil createImageViewFrame:CGRectMake(kUIScreenWidth/2-biaoImg_weidth/2, (kUIScreenHeight*250)/2208, biaoImg_weidth, biaoImg_weidth-10) imageName:@"开锁logo"];
//    biaoImgView.backgroundColor = [UIColor redColor];
    [self.view addSubview:biaoImgView];
    
    UILabel *nameLable = [MyUtil createLabelFrame:CGRectMake(0, CGRectGetMaxY(biaoImgView.frame), kUIScreenWidth, 20) title:@"杭州心安电子科技有限公司" textColor:[UIColor blackColor] textAlignment:NSTextAlignmentCenter font:[UIFont systemFontOfSize:18]];
    [self.view addSubview:nameLable];
    
    
    //手机号
    SinglePhoneNumberView *singlePhoneNumberView = [[SinglePhoneNumberView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(biaoImgView.frame)+(kUIScreenHeight*250)/2208, kUIScreenWidth, 45)];
    [singlePhoneNumberView returnPhone:^(NSString *PhoneNumber) {
        phoneNumberStr = PhoneNumber;
    }];
    [self.view addSubview:singlePhoneNumberView];
    
    //密码
    PasswordView *passwordView = [[PasswordView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(singlePhoneNumberView.frame) + (kUIScreenHeight*60)/2208, kUIScreenWidth, 45)];
    passwordView.textF.placeholder = @"请输入密码";
    [passwordView returnPassword:^(NSString *Password) {
        PasswordStr = Password;
    }];
    [self.view addSubview:passwordView];
    
    //登录
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(3*kSize, CGRectGetMaxY(passwordView.frame) + (kUIScreenHeight*130)/2208, kUIScreenWidth - 6*kSize, 45);
    loginBtn.backgroundColor = btnColor;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"tb_bj"] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.clipsToBounds = YES;
    [loginBtn addTarget:self action:@selector(LoginAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    //忘记密码 验证码登录
    UIButton *lostPasswordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    lostPasswordBtn.frame =  CGRectMake(CGRectGetMaxX(loginBtn.frame)-120, CGRectGetMaxY(loginBtn.frame)+10, 120, 40);
    lostPasswordBtn.titleLabel.font = kfont(14);
    [lostPasswordBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    lostPasswordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;//文字左对齐
    [lostPasswordBtn setTitleColor:RGBColor(130, 0, 223) forState:UIControlStateNormal];
    [lostPasswordBtn addTarget:self action:@selector(LostPasswordAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lostPasswordBtn];
    
    UIButton *resignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    resignBtn.frame =  CGRectMake(CGRectGetMinX(loginBtn.frame)+10, CGRectGetMaxY(loginBtn.frame)+10, 120, 40);
    resignBtn.titleLabel.font = kfont(14);
    [resignBtn setTitle:@"注册" forState:UIControlStateNormal];
    resignBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//文字左对齐
    [resignBtn setTitleColor:RGBColor(130, 0, 223) forState:UIControlStateNormal];
    [resignBtn addTarget:self action:@selector(ResignAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:resignBtn];
    
    
}
#pragma mark   -------------  登录 -----------
-(void)LoginAction{
    if (![BasePublicClass isMobileNumber:phoneNumberStr]) {
        [MBProgressHUD showText:@"手机号错误!!!!!"];
    }else if (!PasswordStr.length){
        
        [MBProgressHUD showText:@"密码不能为空!!!!"];
    }else{
        //登录操作
        NSLog(@"登录操作");
        [self LoginDataHandle];
    }
    
}
#pragma mark  ----------- 忘记密码
-(void)LostPasswordAction{
    NSLog(@"忘记密码");
    ForgetPasswordViewController *forgetPasswordVC = [[ForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:forgetPasswordVC animated:YES];
}




#pragma mark   ------------ 登录接口 ------

-(void)LoginDataHandle{
    
    NSString *urlStr = [NSString stringWithFormat:LOGINDATA,RealmName];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:phoneNumberStr forKey:@"tell"];
    [dic setObject:PasswordStr forKey:@"password"];
    
    [HttpRequest postWithURLString:urlStr parameters:dic success:^(id responseObject) {
        if([responseObject[@"msg"] isEqualToString:@"success"]) {
            [MBProgressHUD showText:@"正在为您登录...."];
            
            NSDictionary *dataDic = [NSDictionary dictionaryWithDictionary:responseObject];
            
            
            
            //存储信息
            [SaveDataMethord SaveLoginDataToLocationWithDic:dataDic.mutableCopy];
            
            
            
           NSDictionary *savedic = [SaveDataMethord readLoginData];
            NSLog(@"%@",savedic);
            //登录
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                
//                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                BaseTabBarController *baseTabbarVC =  [mainStory instantiateViewControllerWithIdentifier:@"BaseTabBarController"];
//                [self presentViewController:baseTabbarVC animated:YES completion:nil];
                
                
                
                MainViewController *rootVC = [[MainViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
                self.view.window.rootViewController = nav;
                
                
            });
        }else{
            [MBProgressHUD showText:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
}

-(void)ResignAction{
    ResignViewController *resignVC = [[ResignViewController alloc] init];
    [self.navigationController pushViewController:resignVC animated:YES];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
